<?php

namespace app\store\controller;

use think\Db;
use think\Request;
use app\common\model\Mail as MailModel;
/**
 * 站内信控制器
 * Class Mail
 * @package app\store\controller
 */
class Mail extends Controller
{
    /*
     * 展示页面
     * */
    public function index() {
        $userData = Db::name('store_user')->where('is_delete',0)->select()->toArray();
        $this->assign('userData',json_encode($userData,true));
        return $this->fetch('index');
    }
    /*
     * 获取消息
     * */
    public function getList(Request $request) {
        $request = $request->post();
        $page = !empty($request['page']) ? $request['page'] : 1;
        $row = !empty($request['row']) ? $request['row'] : 10;
        // 消息只能本人看到
        $store_user_id = $this->store['user']['store_user_id'];
        $count = Db::name('mail')->where('accept_user_id',$store_user_id)->select()->count();
        $data= Db::name('mail')->alias('a')->join('store_user b','a.send_user_id = b.store_user_id')->where('accept_user_id',$store_user_id)->order('is_read','asc')->order('a.update_time','desc')->paginate([
            'list_rows'=> $row,
            'page' => $page,
        ])->toArray();
        foreach ($data['data'] as $k=>$v) {
            $data['data'][$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
            $data['data'][$k]['check'] = false;
        }
        return json_encode([
            'code' => 200,
            'data'=> $data
        ]);
    }
    /*
     * 新增消息
     * */
    public function addMail(Request $request){
        $request = $request->post();
        $store_user_id = $this->store['user']['store_user_id'];
        $flag = 0; // 是否发送给所有人
        if (in_array('-1',$request['accept_user_id'])) {
            $flag = 1;
        }
        if ($flag == 1) {
            // 发送给所有人
            $user_data = Db::name('store_user')->where('is_delete',0)->select()->toArray();
            $new_data = [];
            foreach($user_data as $k=>$v) {
                $new_data[$k]['send_user_id'] = $store_user_id;
                $new_data[$k]['accept_user_id'] = $v['store_user_id'];
                $new_data[$k]['type'] = $request['type'];
                $new_data[$k]['content'] = $request['content'];
            }
            $model = new MailModel();
            $model->addAll($new_data);
        } else {
            // 选中用户
            $new_data = [];
            foreach($request['accept_user_id'] as $k=>$v) {
                $new_data[$k]['send_user_id'] = $store_user_id;
                $new_data[$k]['accept_user_id'] = $v;
                $new_data[$k]['type'] = $request['type'];
                $new_data[$k]['content'] = $request['content'];
            }
            $model = new MailModel();
            $model->addAll($new_data);
        }
        return json_encode([
            'code' => 200,
            'msg' => '发送成功'
        ]);
    }
    /*
     * 清除未读消息
     * */
    public function clearMail(Request $request){
        $request = $request->post();
        $store_user_id = $this->store['user']['store_user_id'];
        $data = [];
        foreach($request['ids'] as $k=>$v) {
            $data[$k]['id'] = $v;
            $data[$k]['is_read'] = 1;
        }
        $model = new MailModel();
        $model->saveAll($data);
        return json_encode([
            'code' => 200,
            'msg' => '发送成功'
        ]);
    }
    /*
     * 获取未读消息数量
     * */
    public function getNUm() {
        $store_user_id = $this->store['user']['store_user_id'];
        $data = Db::name('mail')->where('accept_user_id',$store_user_id)->where('is_read',0)->select()->count();
        return json_encode([
            'code' => 200,
            'data' => $data
        ]);
    }
}
